class Person { }
class Dog { }

const p1: Person = new Person()

// typeof Person获得构造函数的具体类型
// InstanceType构造函数创建出来的实例对象的类型
type HyPerson = InstanceType<typeof Person>
const p2: HyPerson = new Person()

// 通过的创建实例的工具函数时会用到这个InstanceType
function factory<T extends new (...args: any[]) => any>(ctor: T): InstanceType<T> {
  return new ctor()
}

const p3 = factory(Person)
const p4 = factory(Dog)

// 类型体操
type HYInstanceType<T extends new (...args: any[]) => any> = T extends new (...args: any[]) => infer R ? R : never

function factory1<T extends new (...args: any[]) => any>(ctor: T): HYInstanceType<T> {
  return new ctor()
}

const p5 = factory1(Person)
const p6 = factory1(Dog)